package com.projects.android.bus;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.projects.android.dto.RestaurantDTO;
import com.projects.android.utils.Utils;

public class RestaurantBUS {
	public static ArrayList<RestaurantDTO> selectByIDDistrictAndIDFoodType(
			int iddistrict, int idfoodtype) {
		ArrayList<RestaurantDTO> myData = null;
		try {
			Map<String, String> mapUser = new HashMap<String, String>();
			mapUser.put("iddistrict", String.valueOf(iddistrict));
			mapUser.put("idfoodtype", String.valueOf(idfoodtype));
			String url = Utils.buildURL(Utils.SERVICE_URI
					+ "/Restaurant_SelectByIDDistrictAndIDFoodType", mapUser);

			// Send GET request to <service>/GetPlates
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(new URI(url));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();

			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("RestaurantBUS selectByIDDistrictAndIDFoodType", "Response: "
					+ result);

			if (result != "") {
				// Init array objects
				JSONArray arrData = new JSONArray(result);
				myData = new ArrayList<RestaurantDTO>();
				// Loop for all reveived objects
				if (arrData != null) {
					for (int i = 0; i < arrData.length(); i++) {
						JSONObject data = (JSONObject) arrData.get(i);
						RestaurantDTO dto = new RestaurantDTO();
						dto.initObject(data);
						myData.add(dto);
					}
				}
			}
		} catch (Exception e) {
			Log.e("RestaurantBUS selectByIDDistrictAndIDFoodType", e.toString());
		}
		return myData;
	}
}
